﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddLookupColumnToList.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Adds a Lookup Column to a List.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.Column
{
    using System.Collections.Generic;

    using Entities;

    using Engine;

    using SharePoint.Wss.Attributes;

    using Utils;
    using SharePoint.Wss.Lists;

    /// <summary>
    /// The add lookup column to list.
    /// </summary>
    [CollaborisClass(
        Title = "Add Lookup Column to a List", 
        ReleaseStatus = ReleaseStatus.Beta, 
        DisableDocumentation = false, 
        ContactAuthorEmail = "Support@Collaboris.co.uk",
        Summary = "Creates a new <b>list</b>-scoped lookup column pointing to an existing list.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C625}")]
    public class AddLookupColumnToList : WSSActionBase<AddLookupColumnToListInfo>
    {
        #region Helper Methods

        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="AddSiteColumnToListInfo"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, AddLookupColumnToListInfo entity)
        {
            this.Trace.TraceMethodStart("DoAction");
            ValidationHelper.VerifyObjectArgument(context.Site, "context.Site");

            TraceHelper.TraceInfo(TraceSwitch, "AddLookupColumnToList", "Adding a lookup column : {0} from list {1} to column {2} in the list {3}.",
                                                                       entity.SourceListFieldName,
                                                                       entity.SourceListUrl,
                                                                       entity.TargetListFieldName,
                                                                       entity.TargetListUrl);

            SPListHelper.AddLookupFieldToList(entity.TargetListUrl,
                                              entity.TargetListFieldName,
                                              entity.SourceListUrl,
                                              entity.SourceListFieldName,
                                              entity.FieldValueRequired);
            

            this.Trace.TraceMethodEnd("DoAction");
            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
            SafContext context, AddLookupColumnToListInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.SourceListFieldName, "AddLookupColumnToListInfo.SourceListFieldName", ref errors);
            ValidateStringForNull(entity.SourceListUrl, "AddLookupColumnToListInfo.SourceListUrl", ref errors);
            ValidateStringForNull(entity.TargetListFieldName, "AddLookupColumnToListInfo.TargetListFieldName", ref errors);
            ValidateStringForNull(entity.TargetListUrl, "AddLookupColumnToListInfo.TargetListUrl", ref errors);

            // Have we added errors ?
            return errors.Count == currentCount;
        }

        #endregion
    }
}